
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Plot the mass distribution for the specified objects
PRO ANALYZE_MFN_PLOT_MASS,ii,DOX=dox,DOY=doy,DOTIT=dotit
  
  COMMON LAZY_BLOCK, slice,x0,y0,x1,y1,xn0,yn0,xn1,yn1
  
  dox = KEYWORD_SET(dox)
  doy = KEYWORD_SET(doy)
  dotit = KEYWORD_SET(dotit)
  
  FOR it=0,1 DO BEGIN
     
     its = string(it,format="(I0)")
     
     ;; Read in the proper MFN save file
     fn = './masses/save_files/sliced_'+slice[ii]+'_td'+its+'.sav'
     restore,fn
     
     iu = where(sl.usei)
     m = sl.mass[iu]
     plothist,alog10(m),bin=0.1,xarr,yarr,/noplot
     dx = xarr[1]-xarr[0]
     xarr = [xarr[0]-dx,xarr,xarr[-1]+dx]
     yarr = [0,yarr,0]
     
     CASE it OF 
        0: BEGIN
           xtf = dox   ? 'exponent10'                : 'blank_axis'
           xti = dox   ? 'M  [M'+cgSymbol('sun')+']' : ''
           ytf = doy   ? 'exponent10'                : 'blank_axis'
           yti = doy   ? 'N per 0.1 dex'             : ''
           pti = dotit ? 'Slice: '+slice[ii]         : ''

           cgPlot,xarr,yarr,psym=10,xst=5,ytickformat=ytf,$
                  charsize=1.0,xr=[-1.5,6],yr=[0.5,1.d5],/ylog,$
                  title=pti,ytit=yti,/yst
           cgAxis,xaxis=0,xr=10.^(!x.crange),/xst,/save,charsize=1.0,$
                  xtit=xti,/xlog,xtickformat=xtf
           cgAxis,xaxis=1,/xst,xtickformat='blank_axis'
        END
        1: BEGIN
           cgOplot,10.^xarr,yarr,psym=10,color='orange'
        END
     ENDCASE
  ENDFOR
  al_legend,/top,/right,color=['black','orange'],linestyle=0,$
            linsize=0.5,'SampleT!dd!n = '+['0','1'],charsize=0.7,box=0
  al_legend,/top,/left,color=['blu6','grn3'],linestyle=0,$
            linsize=0.5,'SampleT!dd!n = '+['0','1'],charsize=0.7,box=0
  cgOplot,10.^x0,y0,psym=10,color='blu6'
  cgOplot,10.^x1,y1,psym=10,color='grn3'
  
  RETURN
END



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Plot the number density distribution for the specified objects
PRO ANALYZE_MFN_PLOT_NDEN,ii,DOX=dox,DOY=doy,DOTIT=dotit
  
  COMMON LAZY_BLOCK, slice,x0,y0,x1,y1,xn0,yn0,xn1,yn1
  
  dox = KEYWORD_SET(dox)
  doy = KEYWORD_SET(doy)
  dotit = KEYWORD_SET(dotit)
  
  FOR it=0,1 DO BEGIN
     
     its = string(it,format="(I0)")
     
     ;; Read in the proper MFN save file
     fn = './masses/save_files/sliced_'+slice[ii]+'_td'+its+'.sav'
     restore,fn
     
     iu = where(sl.usei)
     n = sl.nden[iu]
     plothist,alog10(n),bin=0.1,xarr,yarr,/noplot
     dx = xarr[1]-xarr[0]
     xarr = [xarr[0]-dx,xarr,xarr[-1]+dx]
     yarr = [0,yarr,0]
     
     CASE it OF 
        0: BEGIN
           xtf = dox   ? 'exponent10'                : 'blank_axis'
           xti = dox   ? 'n  [cm!u-3!n]'             : ''
           ytf = doy   ? 'exponent10'                : 'blank_axis'
           yti = doy   ? 'N per 0.1 dex'             : ''
           pti = dotit ? 'Slice: '+slice[ii]         : ''

           cgPlot,xarr,yarr,psym=10,xst=5,ytickformat=ytf,$
                  charsize=1.0,xr=[1.2,6.2],yr=[0.5,1.d5],/ylog,$
                  title=pti,ytit=yti,/yst
           cgAxis,xaxis=0,xr=10.^(!x.crange),/xst,/save,charsize=1.0,$
                  xtit=xti,/xlog,xtickformat=xtf
           cgAxis,xaxis=1,/xst,xtickformat='blank_axis'
        END
        1: BEGIN
           cgOplot,10.^xarr,yarr,psym=10,color='orange'
        END
     ENDCASE
  ENDFOR
  vline,750.,color='blk4',linestyle=4,/log
  vline,1.d4,color='blk4',linestyle=4,/log
  
  al_legend,/top,/right,color=['black','orange'],linestyle=0,$
            linsize=0.5,'SampleT!dd!n = '+['0','1'],charsize=0.7,box=0
  al_legend,/top,/left,color=['blu6','grn3'],linestyle=0,$
            linsize=0.5,'SampleT!dd!n = '+['0','1'],charsize=0.7,box=0
  cgOplot,10.^xn0,yn0,psym=10,color='blu6'
  cgOplot,10.^xn1,yn1,psym=10,color='grn3'
  
  RETURN
END



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; MAIN ROUTINE
PRO ANALYZE_MFN_SLICES
  
  COMPILE_OPT IDL2, LOGICAL_PREDICATE
  COMMON LAZY_BLOCK, slice,x0,y0,x1,y1,xn0,yn0,xn1,yn1
  
  ;; Get filenames for all slices produced to date
  fns = FILE_SEARCH('./masses/save_files/sliced*sav',count=n)
  
  ;; Extract just the three-character slice ID
  slicej = strmid(fns,strpos(fns[0],'/',/reverse_search)+1)
  slice = strmid(slicej,strpos(slicej[0],'_')+1,3)
  slice = (slice[uniq(slice,sort(slice))])[1:*] ; Remove 'ALL'
  nslice = n_elements(slice)
  
  ;; Load in 'ALL' structures to have an oplot
  restore,'./masses/save_files/sliced_all_td0.sav'
  a0 = sl
  iu = where(a0.usei)
  m = a0.mass[iu]
  plothist,alog10(m),bin=0.1,xarr,yarr,/noplot
  dx = xarr[1]-xarr[0]
  x0 = [xarr[0]-dx,xarr,xarr[-1]+dx]
  y0 = [0,yarr,0]
  n = a0.nden[iu]
  plothist,alog10(n),bin=0.1,xarr,yarr,/noplot
  xn0 = [xarr[0]-dx,xarr,xarr[-1]+dx]
  yn0 = [0,yarr,0]
  
  
  restore,'./masses/save_files/sliced_all_td1.sav'
  a1 = sl
  iu = where(a1.usei)
  m = a1.mass[iu]
  plothist,alog10(m),bin=0.1,xarr,yarr,/noplot
  dx = xarr[1]-xarr[0]
  x1 = [xarr[0]-dx,xarr,xarr[-1]+dx]
  y1 = [0,yarr,0]
  n = a1.nden[iu]
  plothist,alog10(n),bin=0.1,xarr,yarr,/noplot
  xn1 = [xarr[0]-dx,xarr,xarr[-1]+dx]
  yn1 = [0,yarr,0]
  
  
  
  
  ;;====================================================================
  ;; Group by type, and MULTIPLOT!!!
  
  ;;)))))))))))))))))))))))))
  ;; Distinct regions
  ith = where_array(['l30','gem'],slice,nth)
  
  
  myps,'./masses/plots/slice_mfn_regions.eps',xsize=9
  multiplot_xm,[2,1],mpcharsize=1.0,mtitle='Specific Regions',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_MASS,ith[0],/dox,/doy
  cgText,10^(2.0),10,'Gem OB1',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_MASS,ith[1],/dox
  cgText,10^(2.5),10,'l = 30'+cgSymbol('deg'),$
         color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  
  myps,'./masses/plots/slice_nden_regions.eps',xsize=9
  multiplot_xm,[2,1],mpcharsize=1.0,mtitle='Specific Regions',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_NDEN,ith[0],/dox,/doy
  cgText,10^(3.5),10,'Gem OB1',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_NDEN,ith[1],/dox
  cgText,10^(3.5),10,'l = 30'+cgSymbol('deg'),$
         color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  
  ;;)))))))))))))))))))))))))
  ;; Physical Size
  ith = where_array(['rsm','rmd','rlg'],slice,nth)
  
  myps,'./masses/plots/slice_mfn_radius.eps',xsize=13.5
  multiplot_xm,[3,1],mpcharsize=1.0,mtitle='Physical Radius',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_MASS,ith[2],/dox,/doy
  cgText,10^(3.5),10,'R < 0.125 pc',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_MASS,ith[1],/dox
  cgText,10^(1.9),10,'0.125 pc < R < 1.25 pc',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_MASS,ith[0],/dox
  cgText,10^(3.5),10,'R > 1.25 pc',color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  myps,'./masses/plots/slice_nden_radius.eps',xsize=13.5
  multiplot_xm,[3,1],mpcharsize=1.0,mtitle='Physical Radius',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_NDEN,ith[2],/dox,/doy
  cgText,10^(4.5),10,'R < 0.125 pc',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_NDEN,ith[1],/dox
  cgText,10^(3.9),10,'0.125 pc < R < 1.25 pc',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_NDEN,ith[0],/dox
  cgText,10^(3.0),10,'R > 1.25 pc',color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  
  ;;)))))))))))))))))))))))))
  ;; Number density
  ith = where_array(['nsm','nmd','nlg'],slice,nth)
  
  myps,'./masses/plots/slice_mfn_nden.eps',xsize=13.5
  multiplot_xm,[3,1],mpcharsize=1.0,mtitle='Number Density',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_MASS,ith[0],/dox,/doy
  cgText,10^(3.0),10,'n > 10!u4!n cm!u-3!n',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_MASS,ith[1],/dox
  cgText,10^(2.5),10,'750 cm!u-3!n < n < 10!u4!n cm!u-3!n',$
         color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_MASS,ith[2],/dox
  cgText,10^(3.0),10,'n < 750 cm!u-3!n',color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  myps,'./masses/plots/slice_nden_nden.eps',xsize=13.5
  multiplot_xm,[3,1],mpcharsize=1.0,mtitle='Number Density',mtitoffset=-0.5
  ANALYZE_MFN_PLOT_NDEN,ith[0],/dox,/doy
  cgText,10^(3.0),10,'n > 10!u4!n cm!u-3!n',color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_NDEN,ith[1],/dox
  cgText,10^(2.5),10,'750 cm!u-3!n < n < 10!u4!n cm!u-3!n',$
         color='red',align=0.5,charsize=0.9
  multiplot
  ANALYZE_MFN_PLOT_NDEN,ith[2],/dox
  cgText,10^(3.0),10,'n < 750 cm!u-3!n',color='red',align=0.5,charsize=0.9
  myps,/done,/mp
  
  
  ;;)))))))))))))))))))))))))
  ;; StarFormation Activity
  ith = where_array(['sf0','sf1','sf2','sf3'],slice,nth)
  
  myps,'./masses/plots/slice_mfn_sfact.eps',xsize=9,ysize=9
  multiplot_xm,[2,2],mpcharsize=1.0,mtitle='Level of Star Formation Activity',$
               mtitoffset=-0.5
  FOR ji=0,3 DO BEGIN
     ANALYZE_MFN_PLOT_MASS,ith[ji],DOY=(ji MOD 2) EQ 0,DOX=ji GE 2
     cgText,10^(2.5),10,'Group '+string(ji,format="(I0)"),$
            color='red',align=0.5,charsize=0.9
     multiplot
  ENDFOR
  myps,/done,/mp
  
  myps,'./masses/plots/slice_nden_sfact.eps',xsize=9,ysize=9
  multiplot_xm,[2,2],mpcharsize=1.0,mtitle='Level of Star Formation Activity',$
               mtitoffset=-0.5
  FOR ji=0,3 DO BEGIN
     ANALYZE_MFN_PLOT_NDEN,ith[ji],DOY=(ji MOD 2) EQ 0,DOX=ji GE 2
     cgText,10^(3.4),10,'Group '+string(ji,format="(I0)"),$
            color='red',align=0.5,charsize=0.9
     multiplot
  ENDFOR
  myps,/done,/mp
  
  
  ;;)))))))))))))))))))))))))
  ;; Heliocentric Distance
  ith = where_array('dh'+string(indgen(9),format="(I0)"),slice,nth)
  
  myps,'./masses/plots/slice_mfn_dsun.eps',xsize=13.5,ysize=13.5
  multiplot_xm,[3,3],mpcharsize=1.0,mtitle='Heliocentric Distance',$
               mtitoffset=-0.5
  dpos = [[1.8 , 3.0 , 3.4],$
          [3.2 , 0.6 , 0.7],$
          [0.8 , 1.0 , 1.1]]
  FOR ji=0,8 DO BEGIN
     dr = string([ji,ji+1]*2,format="(I0)")
     ANALYZE_MFN_PLOT_MASS,ith[ji],DOY=(ji MOD 3) EQ 0,DOX=ji GE 6
     cgText,10^(dpos[ji]),10,color='red',align=0.5,charsize=0.9,$
            dr[0]+' kpc < d'+cgSymbol('sun')+' < '+dr[1]+' kpc'
     multiplot
  ENDFOR
  myps,/done,/mp
  
  myps,'./masses/plots/slice_nden_dsun.eps',xsize=13.5,ysize=13.5
  multiplot_xm,[3,3],mpcharsize=1.0,mtitle='Heliocentric Distance',$
               mtitoffset=-0.5
  dpos = [[4.0 , 3.5 , 3.4],$
          [3.2 , 2.8 , 2.8],$
          [3.0 , 2.9 , 4.3]]
  FOR ji=0,8 DO BEGIN
     dr = string([ji,ji+1]*2,format="(I0)")
     ANALYZE_MFN_PLOT_NDEN,ith[ji],DOY=(ji MOD 3) EQ 0,DOX=ji GE 6
     cgText,10^(dpos[ji]),10,color='red',align=0.5,charsize=0.9,$
            dr[0]+' kpc < d'+cgSymbol('sun')+' < '+dr[1]+' kpc'
     multiplot
  ENDFOR
  myps,/done,/mp
  
  
  ;;)))))))))))))))))))))))))
  ;; Galactocentric Radius
  ith = where_array('rg'+string(indgen(7)+1,format="(I0)"),slice,nth)
  
  myps,'./masses/plots/slice_mfn_rgal.eps',xsize=13.5,ysize=13.5
  multiplot_xm,[3,3],mpcharsize=1.0,mtitle='Galactocentric Radius',$
               mtitoffset=-0.5
  dpos = [[0.5 , 3.0 , 2.7],$
          [1.7 , 2.7 , 0.5],$
          [0.9 , 1.0 , 1.1]]
  FOR ji=0,6 DO BEGIN
     dr = string([ji+1,ji+2]*2,format="(I0)")
     ANALYZE_MFN_PLOT_MASS,ith[ji],DOY=(ji MOD 3) EQ 0,DOX=ji GE 4
     cgText,10^(dpos[ji]),10,color='red',align=0.5,charsize=0.9,$
            dr[0]+' kpc < R!dgal!n < '+dr[1]+' kpc'
     multiplot
  ENDFOR
  myps,/done,/mp
  
  myps,'./masses/plots/slice_nden_rgal.eps',xsize=13.5,ysize=13.5
  multiplot_xm,[3,3],mpcharsize=1.0,mtitle='Galactocentric Radius',$
               mtitoffset=-0.5
  dpos = [[3.1 , 3.5 , 3.5],$
          [3.5 , 3.3 , 3.0],$
          [4.3 , 3.0 , 3.0]]
  FOR ji=0,6 DO BEGIN
     dr = string([ji+1,ji+2]*2,format="(I0)")
     ANALYZE_MFN_PLOT_NDEN,ith[ji],DOY=(ji MOD 3) EQ 0,DOX=ji GE 4
     cgText,10^(dpos[ji]),10,color='red',align=0.5,charsize=0.9,$
            dr[0]+' kpc < R!dgal!n < '+dr[1]+' kpc'
     multiplot
  ENDFOR
  myps,/done,/mp
  
  
 
END
